package com.caiyigo.baihua.utils;

import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class StringUtils {

	/**
	 * 判断字符串是否为空；true为空，false不为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		String cloneStr = str == null ? "" : str.trim();
		if (null != cloneStr && !"".equals(cloneStr))
			return false;
		return true;
	}

	// 将字符串根据,号转换为数组
	public static String[] split(String source) {
		if (source == null || source.trim().equals(""))
			return null;
		StringTokenizer commaToker = new StringTokenizer(source, ",");
		String[] result = new String[commaToker.countTokens()];
		int i = 0;
		while (commaToker.hasMoreElements()) {
			result[i] = commaToker.nextToken();
			i++;
		}
		return result;
	}

	/**
	 * 一维数组转换成二维数组
	 * 
	 * @param arr
	 *            一维数组
	 * @param colsize
	 *            长度
	 * @param defaultValue
	 * @return 替换后的二维数组
	 */
	public static String[][] transOneToTow(String[] arr, int colsize,
			String defaultValue) {
		int rows = (arr.length + colsize) / colsize;
		String[][] arr2 = new String[rows][colsize];
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < colsize; col++) {
				if (row * colsize + col < arr.length) {
					arr2[row][col] = arr[row * colsize + col];
				} else {
					arr2[row][col] = defaultValue;
				}
			}
		}
		return arr2;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String string = "营，养、食品 ,与  健　  　　康（独立本科段）  ";
		System.out.println(replaceKongke(string));
		System.out.println(toDBC(string));
	}

	/**
	 * 去空格
	 */
	public static String replaceKongke(String string) {
		string = string.replaceAll(String.valueOf((char) 12288), "");
		return string.replaceAll(" ", "");
	}

	/**
	 * 全角转半角
	 */
	public static String toDBC(String string) {
		char[] c = string.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375) {
				c[i] = (char) (c[i] - 65248);
			}
		}
		return new String(c);
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

}
